package com.smbms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.smbms.pojo.Role;
import com.smbms.pojo.User;
import com.smbms.service.RoleService;
import com.smbms.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @RequestMapping(value = "/401.html")
    public String qx(){
        return "/WEB-INF/401.jsp";
    }

    @RequestMapping(value = "/userList.html")
    public String getUserList(Model model,
                              @RequestParam(value = "queryName",required = false) String queryName,
                              @RequestParam(value = "queryUserRole",required = false) Integer queryUserRole,
                              @RequestParam(value = "pageIndex",required = false) Integer currPageNo){
        //页面大小
        int pageSize = 6;
        //当前页码
        if (currPageNo == null){
            currPageNo = 1;
        }

        if (queryUserRole == null) {
            queryUserRole = 0;
        }

        List<Role> roleList = roleService.getRoleList();

        IPage<User> iPageUser = userService.getUserList(queryName,queryUserRole,currPageNo,pageSize);

        List<User> userList = iPageUser.getRecords();

        //总数量(表)
        int totalCount = (int)iPageUser.getTotal();
        //总页数
        int totalPageCount = (int) iPageUser.getPages();


        model.addAttribute("userList",userList);
        model.addAttribute("roleList",roleList);
        model.addAttribute("queryUserName",queryName);
        model.addAttribute("queryUserRole",queryUserRole);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currPageNo);
        return "userlist";
    }
}
